Imports System.IO
Public Class Form1
    Public d As StreamReader						'basically sceio___
    Public o, t, th, f, fi, s, sv, ei, n As Long	'indicies of groups, makes scanning faster
    Public curCod, curElement, endNum As Long		'public vars
    Public words As New ArrayList					'holds dictionary and words
    Public match As Boolean

	'of course, learning will have to be implemented
	'and when you click next and no word is found, it will have to change the char to the next, for example, you type 5 which is "jkl" and it puts K on the screen, but you want J so you hit next.
	
    Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 'not important
        Dim btnClikd As String = sender.text.ToString.Substring(0, 1) 'not important for the port, basically got the first letter of the button clicked to identify which one was clicked
        Select Case btnClikd 'case = switch
            Case "N" 'next
                If match Then
                    screeen.Text = screeen.Text.Substring(0, screeen.Text.Length - curCod.ToString.Length) & getword(curElement)
                    Exit Sub
                End If
            Case "S" 'space
                curCod = 0
                screeen.Text &= " "
                Exit Select
            Case "C" 'clear
                screeen.Text = screeen.Text.Substring(0, screeen.Text.Length - 1)
                If curCod.ToString.Length > 1 Then
                    curCod = CInt(curCod.ToString.Substring(0, curCod.ToString.Length - 1))
                Else
                    curCod = 0
				End If
				Exit Select
			Case else
			    If curCod = 0 Then
					curCod = CInt(btnClikd)
				Else						'for this block, im envying >> and << operators about now ;)
					curCod *= 10
					curCod += CInt(btnClikd)
				End If
                screeen.Text &= btnClikd	'just display something so they know whats going on
                Exit Select
        End Select

        If getword(-1) <> Nothing Then
            screeen.Text = screeen.Text.Substring(0, screeen.Text.Length - curCod.ToString.Length) & getword(-1)
        End If
    End Sub

    Private Sub T9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim temp As Int16
        d = New StreamReader(screeen.Text)	'screeen.text contains URL to dictionary at this point
        While d.EndOfStream = False
            temp = d.Peek					'peeks at the next char without consuming it
            Select Case temp
                Case Asc("2")				'dont worry about asc(), it does nothing but stop the compiler from bitching at me XD
                    If t = 0 Then
                        t = words.Count
                    End If
                    Exit Select
                Case AscW("3")
                    If th = 0 Then
                        th = words.Count
                    End If
                    Exit Select
                Case AscW("4")
                    If f = 0 Then
                        f = words.Count
                    End If
                    Exit Select
                Case AscW("5")				'... shorten this huge repetitive case, maybe?
                    If fi = 0 Then
                        fi = words.Count
                    End If
                    Exit Select
                Case AscW("6")
                    If s = 0 Then
                        s = words.Count
                    End If
                    Exit Select
                Case AscW("7")
                    If sv = 0 Then
                        sv = words.Count
                    End If
                    Exit Select
                Case AscW("8")
                    If ei = 0 Then
                        ei = words.Count
                    End If
                    Exit Select
                Case AscW("9")
                    If n = 0 Then
                        n = words.Count
                    End If
                    Exit Select
            End Select
            o = 0
            words.Add(d.ReadLine)			'add the word to ram
            screeen.Text = Nothing			'get rid of the dictionary URL
        End While
        d.Close()							'releases the file
    End Sub

    Private Function getword(ByVal start As Long) As String
        Dim startnum As Long = CLng(curCod.ToString().Substring(0, 1)) 'gets first number of curCod which is the ID of the word (if you press 223 curCod is 223)
        Dim i, ID As Long
        match = True
        If start = -1 Then	'if its scanning for a word and hasnt found one yet to skip
            Select Case startnum	'shorten this huge case somehow as well.. i have an idea
                Case 1
                    For i = 0 To t
                        ID = words(i).substring(0, words(i).indexof("|")) 'words(index) is (for example) "2|a" 2 being the ID, "a" being the word, this line gets the ID
                        If curCod = ID Then
                            curElement = i
                            endNum = t	'when you click 'next' it will start here so it wont find the same word over and over
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1) ' this line gets the word if the indexs match
                        End If
                    Next
                    Exit Select
                Case 2
                    For i = 0 To th
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = th
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 3
                    For i = th To f
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = f
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 4
                    For i = f To fi
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = fi
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 5
                    For i = fi To s
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = s
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 6
                    For i = s To sv
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = sv
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 7
                    For i = sv To ei
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = ei
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 8
                    For i = ei To n
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = n
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
                Case 9
                    For i = n To words.Count - 1
                        ID = words(i).substring(0, words(i).indexof("|"))
                        If curCod = ID Then
                            curElement = i
                            endNum = words.Count - 1
                            Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                        End If
                    Next
                    Exit Select
            End Select
        Else	'this is if its found a word and is looking for another one
            For i = (start + 1) To endNum
                ID = words(i).substring(0, words(i).indexof("|"))
                If curCod = ID Then
                    curElement = i
                    Return words(i).substring(words(i).indexof("|") + 1, words(i).length - words(i).indexof("|") - 1)
                End If
            Next
            Return getword(-1)
        End If
        match = False
        Return Nothing	'if it doesnt find a word then dont change anything
    End Function
End Class